//
//  JWSquare.h
//  KotH
//
//  Created by James Weatherley on 02/08/2008.
//  Copyright 2008 James Weatherley. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "../env/vector.h"
#import <OpenGLES/ES1/gl.h>

@class JWBox;
@class JWRobot;
@class JWPlayer;
@class JWSentinel;
@class JWTree;

@interface JWSquare : NSObject
{
	
	int x;
	int z;
	float height;
	
	NSInteger number;
	
	BOOL flat;
	JWTree* tree;
	BOOL selected;
	BOOL player;
	BOOL endOfLevel;
	
	JWRobot* robot;
	JWSentinel* sentinel;
	NSMutableArray* boxes;
	
	// The highest box. Usually the final element in boxes.
	// If that box is absorbed, then it is removed from boxes,
	// but remains in topBox until it falls one unit. This allows
	// for smooth falling of things standing on an absorbed box
	// at the top of the pile.
	JWBox* topBox;
	
	vec3_t	vertexA;
	vec3_t	vertexB;
	vec3_t	vertexC;
	vec3_t	vertexD;
	vec3_t	verticesABCD[4];
	
	vec3_t	normal;
	
	GLfloat colour[3];
}

-(id)initWithPositionX:(int)x z:(int)z height:(double)height number:(NSInteger)number;

@property (readonly) int x;
@property (readonly) int z;
@property (readonly) float height;
@property (readonly) NSInteger number;
@property (readonly) NSArray* boxes;
@property (readonly) JWBox* topBox;
@property (readwrite) BOOL flat;
@property (readwrite, assign) JWTree* tree;
@property (readwrite, assign) JWRobot* robot;
@property (readwrite, assign) BOOL player;
@property (readwrite, assign) JWSentinel* sentinel;
@property (readwrite) BOOL selected;
@property (readwrite) BOOL endOfLevel;

-(double)incrementHeight;

-(BOOL)canMoveTo;

-(NSInteger)boxCount;
-(void)addBox:(JWBox*)box;
-(void)removeBox:(JWBox*)box;
-(void)updateTopBox;

#pragma mark Selection
-(void)setSelected:(BOOL)isSelected;
-(BOOL)selected;

#pragma mark Drawing
-(void)setVerticesA:(vec3_t)a B:(vec3_t)b C:(vec3_t)c D:(vec3_t)d;
-(void)getVerticesA:(vec3_t)a B:(vec3_t)b C:(vec3_t)c D:(vec3_t)d;

-(void)setColour:(GLfloat*)theColour;
-(void)drawSelf;

#pragma mark Picking
-(BOOL)intersectWithRayFromPoint:(vec3_t)point direction:(vec3_t)vec;
-(BOOL)intersectBoundingBoxWithRayFromPoint:(vec3_t)point direction:(vec3_t)vec boxesOnly:(BOOL)boxesOnly;

@end
